﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Fluid;
using Fluid.Values;
using Newtonsoft.Json.Linq;

namespace ZYing.LiquidThemeEngine.Json
{
    public class JObjectFluidIndexable : IFluidIndexable
    {
        private readonly JObject _obj;
        private readonly TemplateOptions _options;
        public JObjectFluidIndexable(JObject jObject, TemplateOptions options)
        {
            _obj = jObject;
            _options = options;
        }
        public int Count => _obj.Count;

        public IEnumerable<string> Keys => _obj.Properties().Select(i => i.Name);

        public bool TryGetValue(string name, out FluidValue value)
        {
            if (_obj.TryGetValue(name, out var token))
            {
                value = FluidValue.Create(token, _options);
                return true;
            }
            else
            {
                value = NilValue.Instance;
            }
            return false;
        }
    }
}
